package com.chen.androidstudy.activity

import android.Manifest
import android.content.IntentFilter
import android.os.Bundle
import android.provider.CallLog
import android.telephony.TelephonyManager
import com.base.library.base.BaseVMActivity
import com.base.library.permission.PermissionUtils
import com.chen.androidstudy.R
import com.chen.androidstudy.databinding.ActivitySimLayoutBinding
import com.chen.androidstudy.receiver.SimStateReceiver
import com.permissionx.guolindev.callback.RequestCallback
import dagger.hilt.android.AndroidEntryPoint


/**
 *
 * @Author:        CHong
 * @CreateDate:    2024/5/22 0022
 * @Description: 监听SIM卡
 */
@AndroidEntryPoint
class SimActivity : BaseVMActivity<IntentViewModel, ActivitySimLayoutBinding>() {
    override fun getLayoutId(): Int {
        return R.layout.activity_sim_layout
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mVB.btnGetPermission.setOnClickListener {
            PermissionUtils.requestPermission(this, arrayListOf(Manifest.permission.READ_PHONE_STATE), object : RequestCallback {
                override fun onResult(allGranted: Boolean, grantedList: MutableList<String>, deniedList: MutableList<String>) {
                    if (allGranted){
                        registerSimReceiver()
                    }
                }
            })
        }
        val hasPermission = PermissionUtils.hasPermission(Manifest.permission.READ_PHONE_STATE)
        if (hasPermission){
            registerSimReceiver()
        }
    }
    private var simStateReceiver: SimStateReceiver?= null
    private fun registerSimReceiver(){
        val simStateReceiver = SimStateReceiver()
//        val intentFilter = IntentFilter(TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED)
        val intentFilter = IntentFilter()
//       intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED)
       intentFilter.addAction(TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED)
        registerReceiver(simStateReceiver, intentFilter)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (simStateReceiver != null){
            unregisterReceiver(simStateReceiver)
        }
    }

}